﻿using GrainManage.Core.Provider.Interfaces;
using MySql.Data.MySqlClient;
using System.Data;
using System.Data.Common;

namespace GrainManage.Core.Provider
{
    public class MySqlProvider : IProvider
    {
        private string connectionString;
        public MySqlProvider(string connectionString) { this.connectionString = connectionString; }

        public DbConnection Connection
        {
            get
            {
                return new MySqlConnection(connectionString);
            }
        }

        public DbCommand GetCommand(string cmdText, DbConnection connection)
        {
            var cmd = new MySqlCommand(cmdText, connection as MySqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            return cmd;
        }

        public DbDataAdapter GetAdapter(string selectCmdText)
        {
            var cmd = GetCommand(selectCmdText, Connection);
            return new MySqlDataAdapter(cmd as MySqlCommand);
        }

        public DbParameter GetDbParameter(string paraName, object value, ParameterDirection direction = ParameterDirection.Input)
        {
            var parameter = new MySqlParameter(paraName, value);
            parameter.Direction = direction;
            return parameter;
        }
    }
}
